/**
 * @fileOverview diagram
 * @author huangtonger@aliyun.com
 */

// const G6 = require('@antv/g6');
const G6 = require('../../../../g6/src');
const Page = require('../base/');
const Util = require('./util/');
const ToolMixin = require('./mixin/tool');
const AnchorMixin = require('./mixin/anchor');
const OrbitMixin = require('./mixin/orbit');
const ResizeMixin = require('./mixin/resize');
const OutterMixin = require('./mixin/outter');
const LinkMixin = require('./mixin/link');
const Mixins = [ ToolMixin, OutterMixin, ResizeMixin, AnchorMixin, OrbitMixin, LinkMixin ];

class Diagram extends Page {
  constructor(inputCfg) {
    const cfg = {
      /**
       * shortcut list
       * @type {object}
       */
      shortcut: {
        copy: true,
        paste: true,
        selectAll: true,
        addGroup: true,
        unGroup: true
      },

      /**
       * graph cfg
       * @type {object|undefined}
       */
      graph: {
        minZoom: 0.2,
        maxZoom: 2
      },

      /**
       * 图类构造函数
       * @type {boolean}
       */
      graphConstructor: G6.Graph,

      /**
       * 是否允许悬空边
       * @type {boolean}
       */
      noEndEdge: true
    };
    const mixinCfg = {};
    Util.each(Mixins, Mixin => {
      Util.mix(mixinCfg, Mixin.CFG);
    });
    Util.mix(true, cfg, mixinCfg, inputCfg);
    super(cfg);
    this.isFlow = true;
  }
  _init() {
    super._init();
    Util.each(Mixins, Mixin => {
      Mixin.INIT && this[Mixin.INIT]();
    });
  }
  bindEvent() {
    super.bindEvent();
    this.on('beforeitemactived', ev => {
      const item = ev.item;
      const graph = this.get('_graph');
      const shapeObj = graph.getShapeObj(item);
      const activedOutterStyle = shapeObj.getActivedOutterStyle(item);
      if (item.isNode) {
        this.addOutterShape(item, activedOutterStyle);
      }
    });
    this.on('beforeitemunactived', ev => {
      const item = ev.item;
      if (item.isNode || item.isGroup) {
        this.clearOutterShape(item);
      }
    });
    this.on('beforeitemselected', ev => {
      const item = ev.item;
      const graph = this.get('_graph');
      const shapeObj = graph.getShapeObj(item);
      const selectedOutterStyle = shapeObj.getSelectedOutterStyle(item);
      if (item.isNode) {
        this.addOutterShape(item, selectedOutterStyle);
      } else if (item.isGroup) {
        this.addOutterShape(item, selectedOutterStyle);
      }
    });
    this.on('beforeitemunselected', ev => {
      const item = ev.item;
      if (item.isNode || item.isGroup) {
        this.clearOutterShape(item);
      }
    });
  }
}
Util.each(Mixins, Mixin => {
  Util.mix(Diagram.prototype, Mixin.AUGMENT);
});
Page.setRegister(Diagram, 'diagram', 'page');
module.exports = Diagram;
